package operator.transfer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import entity.Boy;
import entity.Girl;
import entity.People;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import util.Printer;

class MapOperator {

    public static void main(String[] args) {
//        map();
        cast();
    }


    public static void map() {
//        Observable.just(1,2,4)
//                .map(new Function<Integer, String>() {
//                    @Override
//                    public String apply(Integer integer) {
//                        return "this is "+integer.toString();
//                    }
//                }).subscribe(new Consumer<String>() {
//            @Override
//            public void accept(String s) throws Exception {
//                Printer.print(s);
//            }
//        });

        Observable.just(new Date())
                .map(new Function<Date, Long>() {
                    @Override
                    public Long apply(Date date) throws Exception {
                        return date.getTime() + 10 * 60 * 1000;
                    }
                })
                .map(new Function<Long, String>() {
                    @Override
                    public String apply(Long aLong) throws Exception {
                        return new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date(aLong));
                    }
                })
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Printer.print(s);
                    }
                });


    }

    //类似instanceOf
    public static void cast() {
//        Observable.just(1)
//                .cast(Long.class)
//                .subscribe(result -> {
//                    Printer.print(result.getClass().getSimpleName());
//                });
        List<People> peoples = new ArrayList<>();
        peoples.add(new Boy("wangbin"));
        peoples.add(new Girl("xiaoming"));
        Observable.fromIterable(peoples)
                .cast(Boy.class)
                .subscribe(new Consumer<Boy>() {
                    @Override
                    public void accept(Boy boy) throws Exception {
                        Printer.print(boy.name);
                    }
                });
    }


}
